/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package alg;

import entities.Domain;
import java.util.List;
import operators.QueryExecutionPlan;
import util.DwConfiguration;
import static alg.Maintenance.*;
import static alg.Constants.*;

/**
 *
 * @author Dragan
 */
public class Evaluation {

    double queryExecutionCost;
    double viewMaintenanceCost;
    double totalCost;
    DwConfiguration conf;
    List<QueryExecutionPlan> bestPlans;

    public Evaluation(DwConfiguration conf) {
        this.conf = conf;
    }

    public double evaluate() {
        double value = 0;
        QueryEval qe = new QueryEval();
        double queryExecCost = qe.evaluateQueriesExecTime(conf.getDomain(), conf.getQueries());
        value = queryExecCost;
        bestPlans = qe.getBestPlans();

        double maintenanceCost = evaluateMaintanceTime(conf.getDomain());
        if (maintenanceCost > conf.getAllowedMaintenanceCost()) {
            double rho = conf.getRho() * Math.pow((maintenanceCost - conf.getAllowedMaintenanceCost()) / conf.getAllowedMaintenanceCost() * 100, 2);
            rho = rho * 0.01;
            value += rho * value;
        }
        viewMaintenanceCost = maintenanceCost;
        if (value < 0) {
            value = 0;
        }
        queryExecutionCost = queryExecCost;
        totalCost = value;
        // If value is penalyzed too much
        if (value > MAX_VALUE) {
            value = MAX_VALUE;
        }
        return value;
    }

    public Domain getDomain() {
        return conf.getDomain();
    }

    public double getQueryExecutionCost() {
        return queryExecutionCost;
    }

    public double getTotalCost() {
        return totalCost;
    }

    public double getViewMaintenanceCost() {
        return viewMaintenanceCost;
    }

    public List<QueryExecutionPlan> getBestPlans() {
        return bestPlans;
    }

    @Override
    public String toString() {
        String str = "";
        str += "queryCost:" + getQueryExecutionCost() + " maintCost:" + getViewMaintenanceCost() + " totalCost:" + getTotalCost();
        return str;
    }
}
